package com.common.excel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class ExcelModelUtils {

    /**
     * 文件导出
     * 通过网络流下载
     *
     * @param fileName  导出文件名
     * @param data      导出数据
     * @param aClass    模板实体类
     * @param sheetName 工作表名称
     * @param response  HttpServletResponse
     * @param <T>       泛型对象
     */
    public static <T> void exportData(String fileName, String sheetName, Class<?> aClass, List<?> data, HttpServletResponse response) {
        try {
            EasyExcel.write(ExcelExportUtils.getOutputStream(fileName, response), aClass)
                    .registerWriteHandler(getHandler())
                    .sheet(sheetName).doWrite(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件模板和数据导出
     *
     * @param fileName  文件名
     * @param sheetName 表单名
     * @param aClass    模板实体类
     * @param data      数据
     */
    public static void exportData(String fileName, String sheetName, Class<?> aClass, List<?> data) {
        //这里需要指定用哪个class去写，然后写到第一个sheet上，名字为sheet1 然后文件流会自动关闭
        EasyExcel.write(fileName, aClass)
                .registerWriteHandler(getHandler())
                .sheet(sheetName).doWrite(data);
    }

    /**
     * 自定义excel表格样式
     *
     * @return 表格样式
     */
    private static HorizontalCellStyleStrategy getHandler() {
        // 设置excel的头策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 设置背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        // 设置字体大小
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short) 12);
        headWriteCellStyle.setWriteFont(headWriteFont);

        // 设置内容策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 设置字体
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontHeightInPoints((short) 12);
        contentWriteFont.setFontName("宋体");
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        // 设置自动换行
        contentWriteCellStyle.setWrapped(true);
        // 设置垂直居中
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 设置水平居中
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        // 设置单元格的四个边框
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        // 分别实现头样式和内容样式，第一个参数时头样式，第二个参数时内容样式。
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }
}
